<?php
$this->headLink()->prependStylesheet(_STATIC_HTTP . '/www/mobile/sns.css');
if(!function_exists('formatContent')) {
	function formatContent($txt) {
		$view = system_view::getControllerActionViewInstance();
		if(preg_match_all('/\#([^\#]+)\#/i', $txt, $matches)) {
			foreach($matches[1] as $key => $match) {
				$txt = str_replace($matches[0][$key], '<a href="' . $view->url('sns/topic', array('keyword' => $match)) . '">#' . $match . '#</a>', $txt);
			}
		}
		$txt = preg_replace('/\[url=([^\]]+)\]([^\[]+)\[\/url\]/i', '<a href="\1" target="_blank">\2</a>', $txt);
		if(preg_match_all('/[^"]((http|https|ftp)\:\/\/[\w]+\.[a-z]{2,10}[^\s]+)/i', $txt, $matches)) {
			foreach($matches[1] as $key => $match) {
				$txt = str_replace($match, '<a href="' . $match . '" target="_blank">' . $match . '</a>', $txt);
			}
		}
		if(preg_match_all('/\[@([\w\-]+)\|([^\]]+)\]/i', $txt, $matches)) {
			foreach($matches[1] as $key => $match) {
				$n = $matches[2][$key];
				$i = $matches[1][$key];
				$txt = str_replace($matches[0][$key], '<a href="' . $view->url('sns/user', array('uid' => $i)) . '">@' . $n . '</a>', $txt);
			}
		}
		$txt = preg_replace('/^[\r\n]+(.*)/is', '$1', $txt);
		return preg_replace('/[\r\n]+/is', '<br/><br/>', $txt);
	}
}
if(!function_exists('showImages')) {
	function showImages($images, $title) {
		if(empty($images)) return '';
		$str = '<ul class="timages">';
		foreach($images AS $img) {
			$str .= '<li>';
			$str .= '<a href="' . $img['original'] . '" rel="[gallery]" target="_blank" title="' . $title . '">';
			$str .= '<img src="' . $img['thumb'] . '" /></a>';
			$str .= '</li>';
		}
		$str .= '</ul>';
		return $str;
	}
}
foreach((array) $this->tweets->data AS $row) {
?>
<div class="tweets">
<dl>
	<dt><a href="<?php echo $this->url('sns/user', array('uid' => $row->author->id));?>"><img src="<?php echo $row->author->avatar;?>" /></a></dt>
	<dd>
		<div class="commentMain">
			<div class="content">
				<div id="message-<?php echo $row->id;?>">
					<h5><a href="<?php echo $this->url('sns/user', array('uid' => $row->author->id));?>"><?php echo $this->escape($row->author->name);?></a></h5>
					<?php
					if($row->quote) {
						echo $this->language('转播') . ': ';
					}
					echo formatContent($row->content) . showImages($row->images, $this->escape($row->author->name));
					?>
				</div>
				<?php if($row->quote) { ?>
				<dl class="quote">
					<dt><a href="<?php echo $this->url('sns/user', array('uid' => $row->quote->author->id));?>"><img src="<?php echo $row->quote->author->avatar;?>" /></a></dt>
					<dd>
						<div class="commentMain">
							<div class="content">
								<div id="message-<?php echo $row->quote->id;?>">
									<h5><a href="<?php echo $this->url('sns/user', array('uid' => $row->quote->author->id));?>"><?php echo $this->escape($row->quote->author->name);?></a></h5>
									<?php echo formatContent($row->quote->content) . showImages($row->quote->images, $this->escape($row->quote->author->name));?>
								</div>
							</div>
							<div class="bar">
								<a href="#like" onclick="return false;"><?php echo $this->language('赞')?>(<span class="number"><?php echo $row->hot;?></span>)</a>
								<a href="<?php echo $row->quote->source['url'];?>" target="_blank"><?php echo $row->quote->source['name'];?></a>
								<?php echo $this->language('发表于')?>: <em><?php echo $this->date($row->quote->timeline);?></em>
								
							</div>
						</div>
					</dd>
				</dl>
				<?php } ?>
				<?php if($row->quote) {echo '<div class="clear"></div>';}?>
			</div>
			<div class="bar">
				<a href="#like" onclick="return false;"><?php echo $this->language('赞')?>(<span class="number"><?php echo $row->hot;?></span>)</a> 
				<a href="<?php echo $row->source['url'];?>" target="_blank"><?php echo $row->source['name'];?></a> 
				<?php echo $this->language('发表于')?>: <em><?php echo $this->date($row->timeline);?></em>
			</div>
		</div>
	</dd>
</dl>
<?php } ?>
<div class="clear"></div>
<div class="page"><?php 
	echo '<span class="info">';
	if(isset($this->tweets->paginator) && $this->tweets->paginator->total) {
		echo $this->language('总计') . ':' . $this->tweets->paginator->total . ',';
	}
	if(isset($this->tweets->paginator)) {
		echo $this->tweets->paginator->page;
	} else {
		echo '1';
	}
	echo '</span>';
	$router = system_controller_front::getInstance()->getRouter()->getCurrentRouteIndex();
	foreach($this->tweets->paginator->pages AS $text => $q) {
		echo '<a href="' . $this->url($router, array(), $q) . '">' . $this->language($text) . '</a>';
	}
?><code><a href="#top">TOP</a></code></div>
</div>